# -*- coding: utf-8 -*-
"""
@Time ： 2023/9/16 13:26
@Auth ： y.h
@File ：entity_load.py
@IDE ：PyCharm
@Motto：The sooner you start to code, the longer the program will take. —— Roy Carlson
"""
import types
import keyword
import pythonProjectTemplate.Entity.SystemEntity.my_exception as e
from pythonProjectTemplate.Entity.SystemEntity.load_excel_properties import 读取Excel表单的配置 as le

# todo python没有映射,如果以这个方法作为代码生成功能,会消耗很多资源,同时效率低,所以这个方案已过时

# 自动生成实体类
def entity_load(member_arg: list, entity_name: str):
    if keyword.iskeyword(entity_name):
        raise e.keyword_exception(entity_name)
    else:
        # 定义成员变量 开始
        arg_dict = dict()
        arg: str
        i = 0
        for arg in member_arg:
            if keyword.iskeyword(arg):
                raise e.keyword_exception(arg)

            else:
                arg_filed, i = str_filter(arg, i)

                get_f = f"""def get_{arg_filed}(self):    return self.__{arg_filed}"""
                get_module_code = compile(get_f, '', 'exec')
                get_function_code = get_module_code.co_consts[0]
                getter = types.FunctionType(get_function_code, {})

                set_f = f"""def set_{arg_filed}(self,arg):
                    self.__{arg_filed} = arg
                    return self    """
                set_module_code = compile(set_f, '', 'exec')
                set_function_code = set_module_code.co_consts[0]
                setter = types.FunctionType(set_function_code, {})

                arg_dict.update({"__" + arg_filed: None, "get_" + arg_filed: getter, "set_" + arg_filed: setter})

        # 定义成员变量 结束
        temp_entity = type(entity_name, (), arg_dict)

        return temp_entity


def load_empty_entity(entity_name: str):
    if keyword.iskeyword(entity_name):
        raise e.keyword_exception(entity_name)
    else:
        temp_entity = type(entity_name, (), {})

    return temp_entity


def load_entity_attr(attr_list: list, obj):
    for arg in attr_list:
        if keyword.iskeyword(arg):
            raise e.keyword_exception(arg)
        else:
            eval(f"{obj}.{arg} = None")
    return obj


def get_entity_args(entity: type):
    # 获取成员变量列表 开始
    member = list(vars(entity))

    arg_list = []
    for a in range(len(member)):
        # 筛掉python类内置元素
        if member[a][-2:] != "__" and member[a][:4] != "get_" and member[a][:4] != "set_":
            arg_list.append(member[a])
    # 获取成员变量列表 结束
    return arg_list


def get_entity_getFunc(entity: type) -> list:
    # 获取get方法列表 开始
    member = list(vars(entity))
    get_func_list = []
    for a in range(len(member)):
        # 筛掉python类内置元素
        if member[a][-2:] != "__" and member[a][:4] == "get_":
            get_func_list.append(member[a])
    # 获取get方法列表 结束
    return get_func_list


def get_entity_setFunc(entity: type):
    # 获取set方法列表 开始
    member = list(vars(entity))
    set_func_list = []
    for a in range(len(member)):
        # 筛掉python类内置元素
        if member[a][-2:] != "__" and member[a][:4] == "set_":
            set_func_list.append(member[a])
    # 获取set方法列表 结束
    return set_func_list


def str_filter(arg, index):
    arg = str(arg)
    # 非法值列表
    illegal_str = ["Unnamed: "]
    arg: str
    arg = arg.replace(".", "_")
    arg = arg.replace("-", "_")
    if any(element in arg for element in illegal_str):
        index += 1
        return f"Unnamed{index}", index
    else:
        return arg, index


if __name__ == '__main__':
    print(get_entity_getFunc(le))
